﻿using AutoMapper;
using MyMES.Domain.yjb;
using MyMES.Error;
using MyMES.Infrastructure.Interface;

namespace MyMES.Writer.API.FileNlog
{
    public class Nolgs
    {
        private readonly IBaseRepository<Nlog> accreop;
        private readonly IMapper mapper;

        public Nolgs(IBaseRepository<Nlog> accreop, IMapper mapper)
        {
            this.accreop = accreop;
            this.mapper = mapper;
        }

        /// <summary>
        /// 日志调用
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<APIResult<Nlog>> GetNlogs(string UserName, string LogType, string Respsestate, int Duration, string? ExcepMessage)
        {
            try
            {
                APIResult<Nlog> result = new APIResult<Nlog>();
                var log = new Nlog
                {
                    CreateTime = DateTime.Now,
                    UserName = UserName,
                    LogType = LogType,
                    Respsestate = Respsestate,
                    Duration = Duration,
                    ExcepMessage = ExcepMessage,
                    IsDeleted = false,
                };
                var model = mapper.Map<Nlog>(log);
                string sql = "insert into Nlog(LogId,CreateTime,UserName, LogType, Respsestate, Duration, ExcepMessage, IsDeleted) values(@LogId,@CreateTime,@UserName, @LogType, @Respsestate, @Duration, @ExcepMessage, @IsDeleted)";
                await accreop.Insert(model, sql);
                return result;

            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
